package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ReadResult {
    public static boolean isEmptyFile(String path){
        File file = new File(path);
        boolean flag = false;
        if(!file.exists() || file.length() == 0) {
            System.out.println("文件为空！");
            flag = true;
        }
        return flag;
    }

    public static void rename(String dir){
        //文件夹路径
        File folder = new File(dir);
        //判断文件夹是否存在
        if(!folder.exists()){
            System.out.println("文件夹不存在");
        }
        //文件夹下文件
        File file = new File(dir+"\\part-00000");

        //重命名
        File newFile = new File(file.getAbsolutePath() + ".txt");
           file.renameTo(newFile);
        System.out.println("重命名成功！");
    }

    public static void readFile(String path){

        //获取时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());// new Date()为获取当前系统时间

        RatingInfo ri;
        ArrayList<RatingInfo> riList = new ArrayList<RatingInfo>();
        try {
            File file = new File(path);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String strLine = null;

            while(null != (strLine = bufferedReader.readLine())){
                String[] str=strLine.split(",");
                String key = str[0].substring(1);
                if(RatingMap.ratings.containsKey(key)){
                    String r = RatingMap.ratings.get(key);
                    String newV = time+"&"+str[1];
                    String value = r+"@"+newV;
                    RatingMap.ratings.put(key, value);
                }else{
                    RatingMap.ratings.put(key, time+"&"+str[1]);
                }
//                ri = new RatingInfo();
//                ri.setMovieId(str[0]);
//                ri.setRating(Double.valueOf(str[1]));
//                ri.setMovieName(str[2]);
//                riList.add(ri);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
//        return riList;
    }
}
